﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.OleDb;
using System.Data.SqlClient;
using System.Configuration;

namespace Helper
{
    /// <summary>
    /// Connection string helper
    /// </summary>
    public static class ConnectionStringHelper
    {
        /// <summary>
        /// get connection string, use ConnectionString in web.cofig
        /// </summary>
        /// <param name="server">server</param>
        /// <param name="database">database</param>
        /// <returns>return sql server connection string</returns>
        public static string GetConnectionString(string connName)
        {
            return ConfigurationManager.ConnectionStrings[connName].ConnectionString;
        }
        
        /// <summary>
        /// get connection string, use integrate security authentication
        /// </summary>
        /// <param name="server">server</param>
        /// <param name="database">database</param>
        /// <returns>return sql server connection string</returns>
        public static string GetConnectionString(string server, string database)
        {
            return string.Format(@"Data Source={0};Initial Catalog={1};Integrated Security=True"
                , server, database);
        }

        /// <summary>
        /// get connection string, use sql server authentication
        /// </summary>
        /// <param name="server">server</param>
        /// <param name="database">database</param>
        /// <param name="userName">user name</param>
        /// <param name="password">password</param>
        /// <returns>return sql server connection string</returns>
        public static string GetConnectionString(string server, string database, string userName, string password)
        {
            return string.Format(@"Data Source={0};Initial Catalog={1};User ID={2}; pwd={3}"
                , server, database, userName, password);
        }

        /// <summary>
        /// get connection string, use sql server authentication
        /// </summary>
        /// <param name="server">server</param>
        /// <param name="database">database</param>
        /// <param name="userName">user name</param>
        /// <param name="password">password</param>
        /// <param name="failOverPartner">fail over partner</param>
        /// <returns>return sql server connection string</returns>
        public static string GetConnectionString(string server, string database, string userName, string password, string failOverPartner)
        {
            return string.Format(@"Data Source={0};Initial Catalog={1};User ID={2}; pwd={3}; Failover Partner={4}; Network=dbmssocn"
                , server, database, userName, password, failOverPartner);
        }



        /// <summary>
        /// get connection string for access database
        /// </summary>
        /// <param name="accessDatabasePath">access database file path</param>
        /// <returns>return access connection string</returns>
        public static string GetAccessConnectionString(string accessDatabasePath)
        {
            return string.Format(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0}"
                        , accessDatabasePath);
        }

        /// <summary>
        /// Check if the connection string can connect to server.
        /// </summary>
        /// <param name="connectionString">connection string</param>
        /// <returns>true if connection string can connect to server, or false</returns>
        public static bool IsConnectionStringValid(string connectionString)
        {
            if (string.IsNullOrEmpty(connectionString)) return false;

            IDbConnection conn;
            try
            {
                using (conn = new SqlConnection(connectionString))
                {
                    conn.Open();
                    conn.Close();
                }
            }
            catch
            {
                try
                {
                    using (conn = new OleDbConnection(connectionString))
                    {
                        conn.Open();
                        conn.Close();
                    }
                }
                catch
                {

                    return false;
                }
            }
            return true;
        }

    }
}
